package com.nangle.managehost.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nangle.managehost.R;
import com.nangle.managehost.entity.DayThree;
import com.nangle.managehost.entity.MessageDay;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YFB on 2017/4/12.
 */

public class DynamicAdapter extends BaseAdapter{
    private LayoutInflater mInflater;
    private Context context;
    private List<DayThree> mData = new ArrayList<>();
    public DynamicAdapter(Context context){
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }
    public void setData(List<DayThree> mData){
        this.mData = mData;
    }
    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.layout_dynamic, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tvDate = (TextView)convertView.findViewById(R.id.d_date);
            viewHolder.tvContent = (TextView)convertView.findViewById(R.id.d_content);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tvDate.setText(mData.get(position).getDay());
        List<MessageDay> messages = mData.get(position).getMessageDays();
        int count = messages.size();
        StringBuffer stringBuffer = new StringBuffer();
        for(int i = 0;i<count;i++){
            stringBuffer.append(messages.get(i).getTimes()+"  "+messages.get(i).getContent());
            if(i<count-1){
                stringBuffer.append("\n");
            }
        }
        viewHolder.tvContent.setText(stringBuffer.toString());
        return convertView;
    }
    private class ViewHolder {
        private TextView tvDate;
        private TextView tvContent;
    }
}
